/**
 * Created by zhuhuijun on 2017/7/20.
 */
(function (window, angular, undefined) {
    app.service("dbprofileService", dbprofileService);
    dbprofileService.$inject = ['$http', '$q'];// 注入依赖
    function dbprofileService($http, $q) {
        var services = {
            findAll: findAll,
            findOne: findOne,
            save: save,
            update: update,
            remove:remove,
            connectTest:connectTest,
            findTableFields:findTableFields
        }
        return services;

        function findAll() {
            var deferred = $q.defer();
            postSend("dbprofile/list.do").then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;
        }

        function findOne() {

        }

        function save(formObj) {
            var deferred = $q.defer();
            postSend("dbprofile/save.do", formObj).then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;

        }

        function update(formObj) {
            var deferred = $q.defer();
            postSend("dbprofile/update.do", formObj).then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;
        }

        function connectTest(formObj) {
            var deferred = $q.defer();
            postSend("dbprofile/connectTest.do", formObj).then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;
        }

        function remove(id) {
            var deferred = $q.defer();
            postSend("dbprofile/delete.do", {id:id}).then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;
        }

        function findTableFields(id,tableName) {
            var deferred = $q.defer();
            postSend("loadTable/findTableFields.do", {id:id,tableName:tableName}).then(function (response) {
                return deferred.resolve(response)
            })
            return deferred.promise;
        }
        
        function postSend(url, formObj) {
            var deferred = $q.defer();
            $http({
                method: 'post',
                url: url,
                data: formObj
            }).then(function successCallback(response) {
                return deferred.resolve(response.data);
            }, function errorCallback(response) {
                return deferred.reject(response.data);
            });
            return deferred.promise;
        }
    }
})(window, window.angular);